package com.example.demo.common.interceptor;/*
*
* token拦截器
*
*
* */

import com.alibaba.fastjson.JSONObject;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.demo.common.utills.TokenUtil;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class TokenInterceptor implements HandlerInterceptor {

//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//
//        //如果是方法探测，直接通过
//        if (request.getMethod().equals("OPTIONS")){
//            response.setStatus(HttpServletResponse.SC_OK);
//            return true;
//        }
//
//        response.setCharacterEncoding("utf-8");
//
//        //从请求头中取出token
//        String token = request.getHeader(TokenUtil.AUTH_HEADER_KEY);
//        if (!"null".equals(token)){
//            DecodedJWT result = TokenUtil.verify(token);
//            if (ObjectUtils.isEmpty(result)){
//                JSONObject json = new JSONObject();
//                json.put("success","false");
//                json.put("msg","token已过期，未通过拦截器");
//                json.put("code","5000");
//                System.out.println(json);
//                response.getWriter().append(json.toString());
//                return false;
//            }
//            return true;
//
//        }
//        else if("null".equals(token)){
//            String requestURI = request.getRequestURI();
//            if(requestURI.startsWith("/dataPath")||requestURI.startsWith("/sharePath")||requestURI.startsWith("/user/**")||requestURI.startsWith("/DFileLoadDown")||requestURI.startsWith("/SFileLoadDown")){
//                return true;
//            }
//            response.setCharacterEncoding("UTF-8");
//            response.setContentType("application/json; charset=utf-8");
//            try {
//                JSONObject json = new JSONObject();
//                json.put("success","false");
//                json.put("msg","认证失败，未通过拦截器");
//                json.put("code","50000");
//                System.out.println(json);
//                response.getWriter().append(json.toString());
//            }catch (Exception e){
//                e.printStackTrace();
//                response.sendError(500);
//                return false;
//            }
//        }
//        return false;
//    }
//
//
//


}
